package com.sls.web.manager.game.client;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.web.manager.game.constants.ResultCode;
import com.sls.web.manager.game.pojo.ekhui.Account;
import com.sls.web.manager.game.pojo.ekhui.Deposit;
import com.sls.web.manager.game.pojo.ekhui.Trader;
import com.sls.web.manager.pojo.UserInfoVO;

public class AccountClient{
	private static Logger log = LoggerFactory.getLogger(AccountClient.class) ;
	private final static Lock lock = new ReentrantLock() ;
	private volatile static AccountClient instance = null ;
	private AccountClient(){
		super() ;
	}
	public static AccountClient getInstance(){
		if(instance == null){
			try {
				lock.lock();
				if (instance == null) {
					instance = new AccountClient();
				}
			} finally{
				lock.unlock() ;
			}
		}
		return  instance ;
	}
	public Account getAccount(UserInfoVO userInfoVO , Account account){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300009");
		head.setRequestId(UUID.randomUUID().toString());
		
		SocketDTO socketDTO = SocketUtil.send(head, account);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
	    	String body = socketDTO.getBody();
	    	if(StringUtils.isNotBlank(body)){
	    		Gson gson = new Gson();
	    		account = gson.fromJson(body , Account.class); 
	    	}
	    }
		return account ;
	}
	public Account getAccountByTrader(UserInfoVO userInfoVO , Trader trader){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300010");
		head.setRequestId(UUID.randomUUID().toString());
		Account account = null ;
		SocketDTO socketDTO = SocketUtil.send(head, trader);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
	    	String body = socketDTO.getBody();
	    	if(StringUtils.isNotBlank(body)){
	    		Gson gson = new Gson();
	    		account = gson.fromJson(body , Account.class); 
	    	}
	    }
		return account ;
	}

	public boolean updateAccountState(UserInfoVO userInfoVO , Account account){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300020");
		head.setRequestId(UUID.randomUUID().toString());
		SocketDTO socketDTO = SocketUtil.send(head, account);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			Gson gson = new Gson();
			return gson.fromJson(body, Boolean.class) ;
		}
		return false ;
	}
	public List<Account> getAccountList(UserInfoVO userInfoVO , Account account, int pageNo , int pageSize){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300032");
		head.setRequestId(UUID.randomUUID().toString());
		Map<String , Object> paramMap = new HashMap<String , Object>() ;
		paramMap.put("account", account) ;
		paramMap.put("pageNo", pageNo) ;
		paramMap.put("pageSize", pageSize) ;
		SocketDTO socketDTO = SocketUtil.send(head, paramMap);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			Gson gson = new Gson();
			return gson.fromJson(body, new TypeToken<List<Account>>(){}.getType()) ;
		}
		return null ;
	}
	public int getAccountCount(UserInfoVO userInfoVO , Account account){
		Head head = new Head();
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		head.setServiceCode("300033");
		head.setRequestId(UUID.randomUUID().toString());
		SocketDTO socketDTO = SocketUtil.send(head, account);
		if(ResultCode.success.getCode().equals(socketDTO.getHead().getResponseCode())){
			String body = socketDTO.getBody();
			Gson gson = new Gson();
			return gson.fromJson(body, Integer.class) ;
		}
		return 0 ;
	}
}
